document.addEventListener('DOMContentLoaded', () => {
    // 照片轮播功能
    const slides = document.querySelector('.carousel-slides');
    const prevButton = document.querySelector('.carousel-prev');
    const nextButton = document.querySelector('.carousel-next');
    let currentSlide = 0;
    const totalSlides = document.querySelectorAll('.slide').length;

    function updateSlidePosition() {
        slides.style.transform = `translateX(-${currentSlide * 100}%)`;
    }

    prevButton.addEventListener('click', () => {
        currentSlide = (currentSlide - 1 + totalSlides) % totalSlides;
        updateSlidePosition();
    });

    nextButton.addEventListener('click', () => {
        currentSlide = (currentSlide + 1) % totalSlides;
        updateSlidePosition();
    });

    // 自动轮播
    setInterval(() => {
        currentSlide = (currentSlide + 1) % totalSlides;
        updateSlidePosition();
    }, 5000);

    // 创建单个烟花
    function createFirework(x, y) {
        const firework = document.createElement('div');
        firework.className = 'firework';
        firework.style.left = x + 'px';
        firework.style.top = y + 'px';
        document.body.appendChild(firework);

        // 创建烟花粒子
        const particleCount = 30;
        const angleStep = (2 * Math.PI) / particleCount;
        const colors = ['#ff0000', '#ffd700', '#ff69b4', '#00ff00', '#4169e1', '#ff4500'];

        for (let i = 0; i < particleCount; i++) {
            const particle = document.createElement('div');
            particle.className = 'firework-particle';
            
            // 随机颜色
            const color = colors[Math.floor(Math.random() * colors.length)];
            particle.style.backgroundColor = color;
            
            // 随机距离和角度
            const angle = angleStep * i;
            const distance = 50 + Math.random() * 50;
            const tx = Math.cos(angle) * distance;
            const ty = Math.sin(angle) * distance;
            
            particle.style.setProperty('--tx', `${tx}px`);
            particle.style.setProperty('--ty', `${ty}px`);
            
            firework.appendChild(particle);
        }

        // 清理烟花
        setTimeout(() => {
            document.body.removeChild(firework);
        }, 1000);
    }

    // 蛋糕蜡烛交互
    const flame = document.querySelector('.flame');
    const cake = document.querySelector('.cake');
    let isBlown = false;

    cake.addEventListener('click', (event) => {
        if (!isBlown) {
            flame.style.display = 'none';
            isBlown = true;
            
            // 播放许愿动画
            const cakeText = document.querySelector('.cake-text');
            cakeText.textContent = '生日愿望已经发送到天空啦！';
            cakeText.classList.add('animate__animated', 'animate__bounceIn');
            
            // 在点击位置创建烟花
            const rect = cake.getBoundingClientRect();
            const x = rect.left + rect.width / 2;
            const y = rect.top;
            createFirework(x, y);
        } else {
            // 即使蜡烛已经吹灭，点击蛋糕仍然可以发射烟花
            const rect = cake.getBoundingClientRect();
            const x = rect.left + rect.width / 2;
            const y = rect.top;
            createFirework(x, y);
        }
    });

    // 背景音乐控制
    const bgMusic = document.getElementById('bgMusic');
    const musicToggle = document.getElementById('musicToggle');
    let isMusicPlaying = false;

    musicToggle.addEventListener('click', () => {
        if (isMusicPlaying) {
            bgMusic.pause();
            musicToggle.classList.remove('playing');
        } else {
            bgMusic.play();
            musicToggle.classList.add('playing');
        }
        isMusicPlaying = !isMusicPlaying;
    });

    // 页面加载完成后的欢迎动画
    setTimeout(() => {
        const message = document.querySelector('.message');
        message.style.opacity = '1';
    }, 1000);
});
